import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userinfo: null,
    serverTime: 0,
    activeIndex: 'Home',
    token: "",
    avatar: "",
    roles: [],
    permissions: []

  },
  mutations: {
    setUser(state, userinfo) {//自定义改变state初始值的方法，这里面的参数除了state之外还可以再传额外的参数(变量或对象);
      state.userinfo = userinfo;
    },
    setServerTime(state, time) {
      state.serverTime = time
    },
    setActiveIndex(state, val) {
      state.activeIndex = val
    },
    setToken(state, token) {
      state.token = token
    },
    setAvatar(state, avatar) {
      state.avatar = avatar
    },
    setRoles(state, roles) {
      state.roles = roles
    },
    setPermissions(state, permissions) {
      state.permissions = permissions
    },
    /**
    layout (state, payload) {
      state.layout = payload
    },
    token (state, payload) {
      state.token = payload
    },
    btnCode (state, payload) {
      state.btnCode = payload
    },
    user (state, payload) {
      state.user = payload
    },
    menuList (state, payload) {
      state.menuList = payload
    }
     */
  },
  actions: {
    updateUser(context, userinfo) {  //自定义触发mutations里函数的方法，context与store 实例具有相同方法和属性
      context.commit('setUser', userinfo);
    },
    
    updateRoles(context, roles) {
      context.commit('setRoles', roles)
    }
    ,
    updatePermissions(context, permissions) {
      context.commit('setPermissions', permissions)
    }
    ,
    updateServerTime(context, time) {
      context.commit('setServerTime', time);
    },
    updateActiveIndex(context, val) {
      context.commit('setActiveIndex', val)
    },
    updateToken(context, token) {
      context.commit('setToken', token)
    },
    updateAvatar(context, avatar) {
      context.commit('setAvatar', avatar)
    }
  },
  modules: {
  },
  getters: {
    getUser(state) {//方法名随意,主要是来承载变化的userinfo的值
      return state.userinfo
    },
    getRoles(state) {
      return state.roles
    },
    getPermissions(state) {
      return state.permissions
    },
    getServerTime(state) {
      return state.serverTime
    },
    getActiveIndex(state) {
      return state.activeIndex
    },
    getToken(state) {
      return state.token
    },
    getAvatar(state) {
      return state.avatar
    }
  }
})
